package com.xinghou.platform.lucene.main.bean;

import com.xinghou.platform.util.ConfigUtil;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.impl.CloudSolrClient;
import org.apache.solr.client.solrj.impl.HttpSolrClient;

import java.util.HashMap;

/**
 * Created by qingshengzheng on 16/4/26.
 */
public class SolrServerFactory {

    private static  HashMap<String,SolrClient> solrServerMap=new HashMap<String,SolrClient>();

    private static String solr_user=ConfigUtil.getProperty("solr_user");
    private static String solr_store=ConfigUtil.getProperty("solr_store");
    private static String solr_sharedcard=ConfigUtil.getProperty("solr_sharedcard");

    private SolrServerFactory() {

    }

    private static SolrServerFactory solrServerFactory;
    public String exposureSolrUrl;
    public String interestDynamicSolrUrl;
    public String shareCardSolrUrl;
    public String storeSolrUrl;
    public String userSolrUrl;
    public SolrClient exposureServer;
    public SolrClient interestDynamicServer;
    public SolrClient shareCardServer;
    public SolrClient storeServer;
    public SolrClient userServer;

    public static  SolrServerFactory instance() {
        if (solrServerFactory == null) ;
        {
            solrServerFactory = new SolrServerFactory();
            solrServerFactory.init();
        }
        return solrServerFactory;
    }

    public SolrClient getExposureServer() {
        return exposureServer;
    }

    public void setExposureServer(SolrClient exposureServer) {
        this.exposureServer = exposureServer;
    }

    public SolrClient getInterestDynamicServer() {
        return interestDynamicServer;
    }

    public void setInterestDynamicServer(SolrClient interestDynamicServer) {
        this.interestDynamicServer = interestDynamicServer;
    }

    public SolrClient getShareCardServer() {
        return shareCardServer;
    }

    public void setShareCardServer(SolrClient shareCardServer) {
        this.shareCardServer = shareCardServer;
    }

    public SolrClient getStoreServer() {
        return storeServer;
    }

    public void setStoreServer(SolrClient storeServer) {
        this.storeServer = storeServer;
    }

    public SolrClient getUserServer() {
        return userServer;
    }

    public void setUserServer(SolrClient userServer) {
        this.userServer = userServer;
    }

    public void init() {
        String sor_url=ConfigUtil.getProperty("solr_url");
        exposureSolrUrl = sor_url+"xh_exposure";
        interestDynamicSolrUrl=sor_url+"xh_meiji";
        shareCardSolrUrl=sor_url+solr_sharedcard;
        storeSolrUrl=sor_url+solr_store;
        userSolrUrl= sor_url+solr_user;
        exposureServer=getSolrServer(sor_url,"xh_exposure");
        interestDynamicServer=getSolrServer(sor_url,"xh_meiji");
        shareCardServer=getSolrServer(sor_url,solr_sharedcard);
        storeServer=getSolrServer(sor_url,solr_store);
        userServer=getSolrServer(sor_url,solr_user);


    }

    public SolrClient getSolrServer(String url,String collection) {
        if(solrServerMap.get(collection)!=null)
        {
            return solrServerMap.get(collection);
        }
        else return addSolrServer(url,collection);

    }

    public SolrClient addSolrServer(String url, String collection) {
        SolrClient solr = null;
        if (url.startsWith("http")) {
            url=url+collection;
            solr = new HttpSolrClient(url);
            solrServerMap.put(collection, solr);
        } else {
            solr = new CloudSolrClient.Builder().withZkHost(url).build();
            ((CloudSolrClient) solr).setDefaultCollection(collection);
            solrServerMap.put(collection, solr);
        }
        // SolrClient solr = new HttpSolrClient.Builder(url).build();
        return solr;
    }



}
